package com.game;

import java.awt.*;
import java.awt.event.KeyEvent;

public class PlayerOne extends Tank{

    //坦克移动的标志
    private boolean up = false;
    private boolean down = false;
    private boolean left = false;
    private boolean right = false;

    //构造方法
    public PlayerOne(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, gamePanel, upImg, leftImg, rightImg, downImg);
    }

    /*
    * 按下键盘时坦克持续运动
    * */
    public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_A:
                left = true;
                Music.movePlay();
                break;
            case KeyEvent.VK_D:
                right = true;
                Music.movePlay();
                break;
            case KeyEvent.VK_W:
                up = true;
                Music.movePlay();
                break;
            case KeyEvent.VK_S:
                down = true;
                Music.movePlay();
                break;
            case KeyEvent.VK_SPACE:
                if (attackCoolDown){
                    Music.attackPlay();
                }
                attack();
                break;
            default:
                break;
        }
    }
    /*
    * 松开键盘时，坦克停止运动
     * */
    public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_A:
                left = false;
                Music.moveStop();
            case KeyEvent.VK_D:
                right = false;
                Music.moveStop();
                break;
            case KeyEvent.VK_W:
                up = false;
                Music.moveStop();
                break;
            case KeyEvent.VK_S:
                down = false;
                Music.moveStop();
                break;
            case KeyEvent.VK_SPACE:
                break;
            default:
                break;
        }
    }

    /*
    * 调用移动方法
    * */
    public void move(){
        if (up){
            upWard();
        }
        else if (down){
            downWard();
        }
        else if (left){
            leftWard();
        }
        else if (right){
            rightWard();
        }
        else {
            return;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(getImg(),getX(),getY(),getWidth(),getHeight(),null);
        this.move();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(getX(),getY(),getWidth(),getHeight());
    }
}
